package com.v2.k34;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.InstantiationAwareBeanPostProcessor;
import org.springframework.stereotype.Component;

@Component
public class TestInstantiationAwareBeanPostProcessor implements InstantiationAwareBeanPostProcessor {

	// 实例化前和初始化前 只有一次 修改变量有效：
	// 如实例化前修改了，则实例化后和初始化前方法都不会执行；
	// 如实例化前和初始化前都有改变变量的语句，则只有实例化前的语句有效
	@Override
	public Object postProcessBeforeInstantiation(Class<?> beanClass, String beanName) throws BeansException {
		System.out.println("["+beanName+"]实例化前。。。");
		if("userService".equals(beanName)) {
			System.out.println("	["+beanName+"]执行new A()。。。");
			return new A();
		}
		return null;
	}

	@Override
	public boolean postProcessAfterInstantiation(Object bean, String beanName) throws BeansException {
		System.out.println("["+beanName+"]实例化后。。。");
		return false;
	}

	@Override
	public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
		System.out.println("["+beanName+"]初始化前。。。");
//		if("userService".equals(beanName)) {
//			System.out.println("	["+beanName+"]执行new B()。。。");
//			return new B();
//		}
		return null;
	}

	@Override
	public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
		System.out.println("["+beanName+"]初始化后。。。");
		return null;
	}
}
